﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace Beetle.Sl.EditorControl.Core
{
    public abstract class ToolBarItem : ViewModel, IToolBarItem
    {
        private bool _isEnabled = true;
        private bool _isSelected = false;
        private ICommand _Command;
        private ICommand _popupCommand;
        private UIElement _content = null;
        private BitmapImage buttonBmp = new BitmapImage();
        private Uri _imageButtonUri;
        private bool _isOpenPopup;
        private object _toolTip;

        public Editor Editor { get; private set; }


        public abstract string Name
        {
            get;
        }

        public bool IsEnabled
        {
            get
            {
                return this._isEnabled;
            }

            set
            {
                this._isEnabled = value;
                this.RaisePropertyChanged("IsEnabled");
            }
        }

        public object ToolTip
        {
            get
            {
                return this._toolTip;
            }

            set
            {
                this._toolTip = value;
                this.RaisePropertyChanged("ToolTip");
            }
        }

        public bool IsSelected
        {
            get
            {
                return this._isSelected;
            }
            set
            {
                this._isSelected = value;
                this.RaisePropertyChanged("IsSelected");
            }
        }

        public bool IsOpenPopup
        {
            get
            {
                return this._isOpenPopup;
            }
            set
            {
                this._isOpenPopup = value;
                this.RaisePropertyChanged("IsOpenPopup");
            }
        }

        public ICommand Command
        {
            get
            {
                if (this._Command == null)
                {
                    this._Command = new DelegateCommand(this.OnCommand, this.CanExcuteCommand);
                }

                return this._Command;
            }
        }

        public ICommand PopupCommand
        {
            get
            {
                if (this._popupCommand == null)
                {
                    this._popupCommand = new DelegateCommand(this.OnPopupCommand, this.CanExcuteCommand);
                }

                return this._popupCommand;
            }
        }

        public virtual ToolBarItemVisualTypeEnum ToolBarItemVisualType
        {
            get
            {
                return ToolBarItemVisualTypeEnum.Button;
            }
        }

        public virtual UIElement Content
        {
            get
            {
                if (this.ImageButtonUri == null)
                {
                    return null;
                }

                if (this._content == null)
                {
                    this._content = new Image()
                    {
                        Source = this.buttonBmp,
                        Width = 16,
                        Height = 16
                    };
                }

                return this._content;
            }
        }

        public virtual UIElement PopupContent
        {
            get
            {
                return null;
            }
        }

        public virtual Uri ImageButtonUri
        {
            get
            { 
                return this._imageButtonUri;
            }

            set
            {
                this._imageButtonUri = value;
                this.buttonBmp.UriSource = value;
            }
        }

        public virtual void UpdateVisual()
        { }

        public virtual void Load(Editor editor)
        {
            this.Editor = editor;
        }

        public virtual void OnCommand(object arg)
        { }

        public virtual void OnPopupCommand(object arg)
        {
            this.IsSelected = !this.IsSelected;
            this.IsOpenPopup = !this.IsOpenPopup;
        }

        public virtual bool CanExcuteCommand()
        {
            return this.IsEnabled;
        }
    }
}
